//
//  LLOrderDetailView.m
//  UserUse
//
//  Created by Yiwi on 16/10/18.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LLOrderDetailView.h"

typedef NS_ENUM(NSUInteger, LLOrderStatus) {
    //未完成订单
    LLNoPayAndNoMatch,      //没有支付没有匹配司机
    LLPayAndNoMatchNotime,  //支付了未到俩个小时没有匹配司机
    LLPayAndNoMatch,        //支付了到两个小时没有匹配司机
    LLPayAndMatch,          //支付了并且匹配司机显示司机信息
    LLCancalOderAndNoSucess,//取消订单但是钱没有返回
    //订单完成
    LLCancalOderAndSucess,  //取消订单钱已经返回
    LLOderSucess,           //订单已经正常完成
};

@interface LLOrderDetailView(){
    UIView * _topView;
    UIView * _midView;
    LLOrderStatus _orderStatus;//订单状态
    UILabel *_dectionLabel;
    UILabel *_dection;
    UILabel *_dateLabel;
    UILabel *_upCarLabel;
    UILabel *_downCarLabel;
    UILabel *_modeLabel;
    UILabel *_PeopleNumLabel;
    UILabel *_moneyLabel;
    UILabel *_driverLabel;
    UILabel *_iphoneLabel;
    UILabel *_carNum;
}


@end

@implementation LLOrderDetailView

-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = kColoeString(@"f4f4f4");
        [self setupSubView];
    }
    return self;
}
-(void)setupMidView{
    _midView = [UIView new];
    _midView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_midView];
    switch (_orderStatus) {
        case LLNoPayAndNoMatch://没有支付没有匹配司机
            [self setupNODriverSubviewWith:@"请在规定时间内及时进行支付。"];
            [self setupButton:_orderStatus];
            break;
        case LLPayAndNoMatchNotime://支付了未到俩个小时没有匹配司机
            [self setupNODriverSubviewWith:@"请在开车前2小时查询司机信息以及车牌号！"];
            [self setupButton:_orderStatus];
            break;
        case LLPayAndNoMatch://支付了到两个小时没有匹配司机
            [self setupNODriverSubviewWith:@"正在为您选派司机...."];
            [self setupButton:_orderStatus];
            break;
        case LLPayAndMatch://支付了并且匹配司机
            [self setupDriverDetailSubview];
            [self setupButton:_orderStatus];
            break;
        case LLCancalOderAndNoSucess://取消订单但是钱没有返回
             [self setupNODriverSubviewWith:[@"订单已取消," stringByAppendingFormat:@"\n%@",@"资金会在1-7个工作日内返回您的账号！"]];
            break;
        case LLCancalOderAndSucess://取消订单钱已经返回
             [self setupNODriverSubviewWith:[@"订单已取消," stringByAppendingFormat:@"\n%@",@"资金已成功返回您的账户！"]];
            break;
        case LLOderSucess://订单已经正常完成
            break;

    }
}

-(void)setupButton:(LLOrderStatus)status{
    
    
    UIButton * completeBt = [UIButton new];
    completeBt.backgroundColor = kColoeString(@"#ff9a02");
    completeBt.layer.masksToBounds = YES;
    completeBt.layer.cornerRadius = kScaleNum(4);
    
    if (status == LLNoPayAndNoMatch) {
        [completeBt setTitle:@"立即支付" forState:UIControlStateNormal];
        [completeBt addTarget:self action:@selector(pay) forControlEvents:UIControlEventTouchUpInside];
    }
    else
    {
        [completeBt setTitle:@"订单完成" forState:UIControlStateNormal];
        [completeBt addTarget:self action:@selector(complete) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self addSubview:completeBt];
    [completeBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(_midView.mas_bottom).offset(kScaleNum(10));
        make.width.equalTo(@(kScaleNum(260)));
        make.height.equalTo(@(kScaleNum(35)));
    }];
    
    UIButton * concalBt = [UIButton new];
    concalBt.backgroundColor = [UIColor whiteColor];
    concalBt.layer.masksToBounds = YES;
    concalBt.layer.cornerRadius = kScaleNum(4);
    [concalBt setTitle:@"取消订单" forState:UIControlStateNormal];
    [concalBt setTitleColor:kColoeString(@"#333333") forState:UIControlStateNormal];
    [concalBt addTarget:self action:@selector(concal) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:concalBt];
    [concalBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(completeBt.mas_bottom).offset(kScaleNum(15));
        make.width.equalTo(@(kScaleNum(260)));
        make.height.equalTo(@(kScaleNum(35)));
    }];
    
}
-(void)pay{



}
-(void)complete{
    [self.delegate complete];
}
-(void)concal{
    [self.delegate concal];
}

-(void)setupOrderMessageWithOrderList:(LLOrderListModel*)orderList{
    
    _orderStatus = [self getOrderStatusWithOrderList:orderList];
    _modeLabel.text = [NSString stringWithFormat:@"拼车方式： %@",orderList.context.ordertype];
    _PeopleNumLabel.text = [NSString stringWithFormat:@"拼车人数： %@",orderList.context.ordernum];
    int orderNum = [orderList.context.ordernum intValue];
    if (orderNum == 0) {
        orderNum = 1;
    }
    _moneyLabel.text = [NSString stringWithFormat:@"%@元",[NSString stringWithFormat:@"%d",[orderList.context.ordermoney intValue]/orderNum]];;
    _dection.text = orderList.titel.route_s;
    _dectionLabel.text = orderList.titel.route_e;
    NSString * timeString = [NSString stringWithFormat:@"%@   %@",orderList.titel.s_date,orderList.titel.s_time];
    _dateLabel.text = timeString;
    _upCarLabel.text = [NSString stringWithFormat:@"上车地点： %@",orderList.titel.origin];
    _downCarLabel.text = [NSString stringWithFormat:@"下车地点： %@",orderList.titel.destination];
    
    [self setupMidView];
    
}

-(LLOrderStatus)getOrderStatusWithOrderList:(LLOrderListModel*)orderList{
    if ([orderList.orderInfo.orderStatus isEqualToString:@"UPU"]) {
        return LLNoPayAndNoMatch;//未支付未拼单
    }else if ([orderList.orderInfo.orderStatus isEqualToString:@"PU"]){
        if ([orderList.orderInfo.orderTranslate isEqualToString:@"未拼单"]&&[orderList.orderInfo.payTranslate isEqualToString:@"未支付"]) {
            return LLNoPayAndNoMatch;//未支付未拼单
        }
        if ([orderList.orderInfo.orderTranslate isEqualToString:@"未拼单"]&&[orderList.orderInfo.payTranslate isEqualToString:@"已支付"]) {
            return LLPayAndNoMatchNotime;//支付未到两个小时未拼单
        }
        return LLPayAndNoMatch;//已支付两小内未拼单
    }else if ([orderList.orderInfo.orderStatus isEqualToString:@"PA"]){
        return LLPayAndMatch;//支付拼单成功
    }else if ([orderList.orderInfo.orderStatus isEqualToString:@"F"]){
        return LLOderSucess;//订单按流程成功
    }else if ([orderList.orderInfo.orderStatus isEqualToString:@"C"]){//取消订单
        if ([orderList.orderInfo.payStatus isEqualToString:@"2003"]) {
            return LLCancalOderAndNoSucess;//取消订单未返回钱
        }else if ([orderList.orderInfo.payStatus isEqualToString:@"2004"]){
            return LLCancalOderAndSucess;//取消之后钱已经返回
        }
    }
    return 0;
}
//判断是不是开车前的两小时
-(BOOL)intervalSinceNow: (NSNumber *) theDate
{
    NSDate *d= [NSDate dateWithTimeIntervalSince1970:[theDate doubleValue] / 1000];
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    
    NSTimeInterval cha=late-now;
    
    if (cha/(3600*2)>1)
    {
        return NO;
    }
    return YES;
}

-(void)setupSubView{
    
    _topView = [UIView new];
    [self addSubview:_topView];
    _topView.backgroundColor = [UIColor whiteColor];
    
    UIView * midLine = [UIView new];
    midLine.backgroundColor = kColoeString(@"#dedede");
    [_topView addSubview:midLine];
    
    UIView * bottomLine = [UIView new];
    bottomLine.backgroundColor = kColoeString(@"#dedede");
    [_topView addSubview:bottomLine];
    
    UILabel * money = [UILabel new];
    money.textColor = kColoeString(@"#666666");
    money.text = @"拼车费用：";
    money.font = [UIFont systemFontOfSize:12];
    [_topView addSubview:money];
    
    UIImageView * arrowLine = [UIImageView new];
    arrowLine.image = [UIImage imageNamed:@"shortArrow"];
    [_topView addSubview:arrowLine];
    
    _dectionLabel = [UILabel new];
    _dectionLabel.textColor = kColoeString(@"#333333");
    _dectionLabel.font = [UIFont systemFontOfSize:15];
    [_topView addSubview:_dectionLabel];
    
    _dection = [UILabel new];
    _dection.textColor = kColoeString(@"#333333");
    _dection.font = [UIFont systemFontOfSize:15];
    [_topView addSubview:_dection];
    
    _dateLabel = [UILabel new];
    _dateLabel.font = [UIFont systemFontOfSize:14];
    _dateLabel.textColor = kColoeString(@"#666666");
    [_topView addSubview:_dateLabel];
    
    UIImageView * upIV = [UIImageView new];
    upIV.image = [UIImage imageNamed:@"getOffMark"];
    [_topView addSubview:upIV];
    
    UIImageView * downIV = [UIImageView new];
    downIV.image = [UIImage imageNamed:@"getoff"];
    [_topView addSubview:downIV];
    
    _upCarLabel =[UILabel new];
    _upCarLabel.textColor = kColoeString(@"#666666");
    _upCarLabel.font = [UIFont systemFontOfSize:12];
    [_topView addSubview:_upCarLabel];
    
    _downCarLabel = [UILabel new];
    _downCarLabel.textColor = kColoeString(@"#666666");
    _downCarLabel.font = [UIFont systemFontOfSize:12];
    [_topView addSubview:_downCarLabel];
    
    _modeLabel = [UILabel new];
    _modeLabel.textColor = kColoeString(@"#666666");
    _modeLabel.font = [UIFont systemFontOfSize:12];
    [_topView addSubview:_modeLabel];
    
    _PeopleNumLabel = [UILabel new];
    _PeopleNumLabel.textColor = kColoeString(@"#666666");
    _PeopleNumLabel.font = [UIFont systemFontOfSize:12];
    [_topView addSubview:_PeopleNumLabel];
    
    _moneyLabel = [UILabel new];
    _moneyLabel.textColor = kColoeString(@"#ff3333");
    _moneyLabel.font = [UIFont systemFontOfSize:12];
    [_topView addSubview:_moneyLabel];
    
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.equalTo(@(kScaleNum(175)));
    }];
    
    [arrowLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topView.mas_top).offset(kScaleNum(17));
        make.centerX.equalTo(_topView.mas_centerX);
        make.width.equalTo(@(kScaleNum(50)));
        make.height.equalTo(@(kScaleNum(10)));
    }];
    
    [_dectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(arrowLine.mas_left).offset(-kScaleNum(10));
        make.centerY.mas_equalTo(arrowLine.mas_centerY);
        make.width.lessThanOrEqualTo(@(kScaleNum(100)));
    }];
    
    [_dection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(arrowLine.mas_right).offset(kScaleNum(10));
        make.centerY.mas_equalTo(arrowLine.mas_centerY);
        make.width.lessThanOrEqualTo(@(kScaleNum(100)));
        
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dectionLabel.mas_bottom).offset(kScaleNum(12));
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(250)));
    }];
    
    [upIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dateLabel.mas_bottom).offset(kScaleNum(10));
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(25));
        make.width.height.equalTo(@(kScaleNum(7)));
    }];
    
    [downIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upIV.mas_bottom).offset(kScaleNum(14));
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(25));
        make.width.height.equalTo(@(kScaleNum(7)));
    }];
    
    [_upCarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(upIV.mas_right).offset(kScaleNum(2.5));
        make.centerY.equalTo(upIV.mas_centerY);
        make.width.lessThanOrEqualTo(@(kScaleNum(200)));
    }];
    
    [_downCarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(downIV.mas_right).offset(kScaleNum(2.5));
        make.centerY.equalTo(downIV.mas_centerY);
        make.width.lessThanOrEqualTo(@(kScaleNum(300)));
    }];
    
    [midLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(10));
        make.right.equalTo(_topView.mas_right).offset(-kScaleNum(10));
        make.top.equalTo(_topView.mas_top).offset(kScaleNum(110));
        make.height.equalTo(@(kScaleNum(.5)));
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_topView);
        make.bottom.equalTo(_topView.mas_bottom).offset(-kScaleNum(.5));
        make.height.equalTo(@(kScaleNum(.5)));
    }];
    
    [_modeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midLine.mas_bottom).offset(kScaleNum(10));
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(250)));
    }];
    
    [_PeopleNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_modeLabel.mas_bottom).offset(kScaleNum(5));
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(250)));
    }];
    
    [money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_PeopleNumLabel.mas_bottom).offset(kScaleNum(5));
        make.left.equalTo(_topView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(62)));
    }];
    
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_PeopleNumLabel.mas_bottom).offset(kScaleNum(5));
        make.left.equalTo(money.mas_right).offset(kScaleNum(2));
        make.width.lessThanOrEqualTo(@(kScaleNum(55)));
    }];
}

-(void)setupNODriverSubviewWith:(NSString*)title{
    UILabel * titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.textColor = kColoeString(@"#333333");
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.numberOfLines = 2;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [_midView addSubview:titleLabel];
    
    [_midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topView.mas_bottom).offset(kScaleNum(10));
        make.left.right.mas_equalTo(0);
        make.height.equalTo(@(kScaleNum(50)));
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_midView);
    }];

}
-(void)setupDriverDetailSubview{
    [_midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topView.mas_bottom).offset(kScaleNum(10));
        make.left.right.mas_equalTo(0);
        make.height.equalTo(@(kScaleNum(65)));
    }];
    
    _driverLabel = [UILabel new];
    _driverLabel.textColor = kColoeString(@"#666666");
    _driverLabel.font = [UIFont systemFontOfSize:12];
    [_midView addSubview:_driverLabel];
    
    _iphoneLabel = [UILabel new];
    _iphoneLabel.textColor = kColoeString(@"#666666");
    _iphoneLabel.font = [UIFont systemFontOfSize:12];
    [_midView addSubview:_iphoneLabel];
    
    _carNum = [UILabel new];
    _carNum.textColor = kColoeString(@"#666666");
    _carNum.font = [UIFont systemFontOfSize:12];
    [_midView addSubview:_carNum];
    
    [_driverLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_midView.mas_top).offset(kScaleNum(10));
        make.left.equalTo(_midView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(250)));
    }];
    
    [_iphoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_driverLabel.mas_bottom).offset(kScaleNum(5));
        make.left.equalTo(_midView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(250)));
    }];
    
    [_carNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iphoneLabel.mas_bottom).offset(kScaleNum(5));
        make.left.equalTo(_midView.mas_left).offset(kScaleNum(25));
        make.width.lessThanOrEqualTo(@(kScaleNum(250)));
    }];
#warning 待写逻辑 ：司机信息接口没有待写

    [self setupDriverMessage];
}
-(void)setupDriverMessage{
    _driverLabel.text = @"接单司机： 李天白";
    _iphoneLabel.text = @"联系方式： 15823958456";
    _carNum.text = @"车牌号码： 豫A233423";
}
@end
